package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.common.req.IdParam;
import com.example.common.resp.Result;
import com.example.springboot.dto.SubmenuResp;
import com.example.springboot.entity.MenuEntity;
import com.example.springboot.entity.RoutesEntity;
import com.example.springboot.entity.SubmenuEntity;
import com.example.springboot.model.SubmenuSaveParam;
import com.example.springboot.service.MenuService;
import com.example.springboot.service.RoutesService;
import com.example.springboot.service.SubmenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 一级路由表 前端控制器
 * </p>
 *
 * @author he
 * @since 2023-11-16
 */
@Tag(name = "一级路由")
@RestController
@RequestMapping("/submenuEntity")
public class SubmenuController {

    @Autowired
    private SubmenuService submenuService;
    @Autowired
    private RoutesService routesService;
    @Autowired
    private MenuService menuService;

    /**
     * @param param
     * @return
     */
    @Operation(summary = "是否存在")
    @PostMapping("/exist")
    public Integer exist(@RequestBody @Validated IdParam param) {
        QueryWrapper<SubmenuEntity> submenuEntityQueryWrapper = new QueryWrapper<>();
        submenuEntityQueryWrapper.eq("id", param.getId());
        if (submenuService.count(submenuEntityQueryWrapper) == 0) {
            return 0;
        }
        return 1;
    }

    @Operation(summary = "路由列表")
    @GetMapping("/list")
    public Result<List<SubmenuResp>> list() {
        List<SubmenuEntity> submenuEntityList = submenuService.list();
        ArrayList<SubmenuResp> submenuRespArrayList = new ArrayList<>();
        for (int i = 0; i < submenuEntityList.size(); i++) {
            if (submenuEntityList.get(i).getId() != 0) {
                SubmenuEntity submenu = submenuEntityList.get(i);
                SubmenuResp submenuResp = new SubmenuResp();
                BeanUtils.copyProperties(submenu, submenuResp);
                submenuRespArrayList.add(submenuResp);
            }
        }
        for (int i = 0; i < submenuRespArrayList.size(); i++) {
            SubmenuResp submenuResp = submenuRespArrayList.get(i);
            //查找routes
            QueryWrapper<RoutesEntity> routesEntityQueryWrapper = new QueryWrapper<>();
            routesEntityQueryWrapper.eq("submenuId", submenuResp.getId());
            List<RoutesEntity> routesEntityList = routesService.list(routesEntityQueryWrapper);
            //查找menu
            ArrayList<MenuEntity> menuEntityArrayList = new ArrayList<>();
            for (int j = 0; j < routesEntityList.size(); j++) {
                RoutesEntity routesEntity = routesEntityList.get(j);
                QueryWrapper<MenuEntity> menuEntityQueryWrapper = new QueryWrapper<>();
                menuEntityQueryWrapper.eq("id", routesEntity.getMenuId());
                MenuEntity menuEntity = menuService.getOne(menuEntityQueryWrapper);
                menuEntityArrayList.add(menuEntity);
            }
            submenuResp.setMenuEntityList(menuEntityArrayList);
        }
        return Result.success(submenuRespArrayList);
    }

    @Operation(summary = "根据id查找")
    @PostMapping("/detail")
    public Result<SubmenuResp> detail(@RequestBody @Validated IdParam param) {
        //ID是否存在
        if (exist(param) == 0) {
            return Result.failure("一级路由不存在");
        }
        SubmenuResp submenuResp = new SubmenuResp();
        SubmenuEntity submenuEntity = submenuService.getById(param.getId());
        BeanUtils.copyProperties(submenuEntity, submenuResp);
        //查找routes
        QueryWrapper<RoutesEntity> routesEntityQueryWrapper = new QueryWrapper<>();
        routesEntityQueryWrapper.eq("submenuId", submenuEntity.getId());
        List<RoutesEntity> routesEntityList = routesService.list(routesEntityQueryWrapper);
        //查找menu
        ArrayList<MenuEntity> menuEntityArrayList = new ArrayList<>();
        for (int i = 0; i < routesEntityList.size(); i++) {
            RoutesEntity routesEntity = routesEntityList.get(i);
            QueryWrapper<MenuEntity> menuEntityQueryWrapper = new QueryWrapper<>();
            menuEntityQueryWrapper.eq("id", routesEntity.getMenuId());
            MenuEntity menuEntity = menuService.getOne(menuEntityQueryWrapper);
            menuEntityArrayList.add(menuEntity);
        }
        submenuResp.setMenuEntityList(menuEntityArrayList);
        return Result.success(submenuResp);
    }

    @Operation(summary = "保存或修改")
    @PostMapping("/save")
    public Result save(@RequestBody @Validated SubmenuSaveParam param) {
        SubmenuEntity submenuEntity = new SubmenuEntity();
        BeanUtils.copyProperties(param, submenuEntity);
        if (submenuEntity.getId() == 0) {
            submenuEntity.setId(null);
            if (submenuService.save(submenuEntity)) {
                return Result.success();
            }
            return Result.failure("保存失败，请稍后重试");
        } else {
            if (submenuService.updateById(submenuEntity)) {
                return Result.success();
            }
            return Result.failure("修改失败，请稍后重试");
        }
    }

    @Operation(summary = "根据id删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody @Validated IdParam param) {
        //ID是否存在
        if (exist(param) == 0) {
            return Result.failure("一级路由不存在");
        }
        //删除
        if (submenuService.removeById(param.getId())) {
            return Result.success();
        } else {
            return Result.failure("删除失败，请稍后重试");
        }
    }

}
